import asyncio
from pyppeteer import launch


async def main():
    # 浏览器 启动参数
    start_parm = {
        # 启动chrome的路径
        "executablePath": r"C:\Users\Gy\AppData\Local\pyppeteer\pyppeteer\local-chromium\588429\chrome-win32\chrome.exe",
        # 关闭无头浏览器
        "headless": False,
        "args": [
            '--disable-infobars',  # 关闭自动化提示框
            # '--window-size=1920,1080',  # 窗口大小
            '--log-level=30',  # 日志保存等级， 建议设置越好越好，要不然生成的日志占用的空间会很大 30为warning级别
            '--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36',  # UA
            '--no-sandbox',  # 关闭沙盒模式
            '--start-maximized',  # 窗口最大化模式
            # '--proxy-server=http://localhost:8888'  # 代理
            # r'userDataDir=D:\project_demo\python_demo\spider_demo\JavaScript 逆向系列课\userdata'  # 用户文件地址
        ],
        'dumpio': True,  # 减少内存消耗
    }
    # 使用launch方法调用浏览器，其参数可以传递关键字参数也可以传递字典。
    browser = await launch(**start_parm)
    # 打开一个页面
    page = await browser.newPage()
    await page.setViewport({'width': 1366, 'height': 768})   # 设置页面的大小
#     # 反爬js
#     js_text = """
# () =>{
#     Object.defineProperties(navigator,{ webdriver:{ get: () => false } });
#     window.navigator.chrome = { runtime: {},  };
#     Object.defineProperty(navigator, 'languages', { get: () => ['en-US', 'en'] });
#     Object.defineProperty(navigator, 'plugins', { get: () => [1, 2, 3, 4, 5,6], });
#  }
#     """
#     await page.evaluateOnNewDocument(js_text)  # 本页刷新后值不变，自动执行js

    await page.evaluateOnNewDocument(
        '''() =>{ Object.defineProperties(navigator,{ webdriver:{ get: () => false } }) }''')
    await page.evaluateOnNewDocument('''() =>{ window.navigator.chrome = { runtime: {},  }; }''')
    await page.evaluateOnNewDocument('''() =>{ Object.defineProperty(navigator, 'languages', { get: () => ['en-US', 'en'] }); }''')
    await page.evaluateOnNewDocument('''() =>{ Object.defineProperty(navigator, 'plugins', { get: () => [1, 2, 3, 4, 5,6], }); }''')
    # 打开链接
    await page.goto('http://app1.nmpa.gov.cn/datasearchcnda/face3/base.jsp?tableId=26&tableName=TABLE26&title=%B9%FA%B2%FA%D2%BD%C1%C6%C6%F7%D0%B5%B2%FA%C6%B7%A3%A8%D7%A2%B2%E1%A3%A9&bcId=152904417281669781044048234789')

    # await page.type('#kw', "aaaa")
    # await page.click('#Submit')
    await page.waitFor(1000)
    page_source = await page.content()

    list_url = await page.xpath('//tbody/tr/td[@height="30"]/p/a')
    for u in list_url:
        title = await (await u.getProperty('textContent')).jsonValue()
        url_child = await (await u.getProperty('href')).jsonValue()
        await u.click()
        await asyncio.sleep(1)
        await page.evaluateOnNewDocument(
            '''() =>{ Object.defineProperties(navigator,{ webdriver:{ get: () => false } }) }''')
        await page.evaluateOnNewDocument('''() =>{ window.navigator.chrome = { runtime: {},  }; }''')
        await page.evaluateOnNewDocument('''() =>{ Object.defineProperty(navigator, 'languages', { get: () => ['en-US', 'en'] }); }''')
        await page.evaluateOnNewDocument('''() =>{ Object.defineProperty(navigator, 'plugins', { get: () => [1, 2, 3, 4, 5,6], }); }''')
        # btn_back = await page.xpath('//img[contains(@src,"images/search_back.gif")]')
        page_source = await page.content()
        # await asyncio.sleep(1)
        # btn_back = await page.xpath('//tr[@height="35"]/td')

        btn_back = await page.xpath('//img[contains(@src,"images/search_back.gif")]')
        await btn_back[0].click()
        page.mouse

        pass
    # 下一页
    button_pre = await page.xpath('//tr/td[4]/img')
    await button_pre[0].click()

    await asyncio.sleep(2)

    await browser.close()

# 调用
asyncio.get_event_loop().run_until_complete(main())